home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / SoundApps / Patchmix / Source / Evp.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  5.7 KB  |  243 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Evp.h"
  5. #import "Instrum.h"
  6. #import <appkit/graphics.h>
  7. #import "Statement.h"
  8.  
  9. @implementation Evp
  10.  
  11. + initialize
  12. {
  13.        image = [NXImage findImageNamed:"Evp"];
  14.     riseOffset.x = 0;
  15.     riseOffset.y = 26;
  16.     ampOffset.x = 17;
  17.     ampOffset.y = 41;
  18.     durOffset.x = 38;
  19.     durOffset.y = 41;
  20.     decayOffset.x = 57;
  21.     decayOffset.y = 26;
  22.     outOffset.x = 29;
  23.     outOffset.y = 8;
  24.     
  25.     return self;
  26. }
  27.  
  28. - init
  29. {
  30.     written = NO;
  31.     size.width = 80.;
  32.     size.height = 80.;
  33.     center.x = size.width/2.;
  34.     center.y = size.height/2.;
  35.     strcpy(type,"Evp");
  36.     strcpy(name,"evp");
  37.     
  38.     amp = [[Param alloc] init:self :&Offset];
  39.     rise = [[Param alloc] init:self :&riseOffset];
  40.     dur = [[Param alloc] init:self :&durOffset];
  41.     decay = [[Param alloc] init:self :&decayOffset];
  42.     out = [[Param alloc] init:self :&outOffset];
  43.  
  44.     [rise setValue:".1"];    // set non-"1" default values
  45.     [dur setValue:"p[1]"];    
  46.     [decay setValue:".1"];    
  47.     [amp setTitle:"Amp:"];
  48.     [rise setTitle:"Rise:"];
  49.     [dur setTitle:"Dur:"];
  50.     [decay setTitle:"Decay:"];
  51.     [out setTitle:"Out:"];
  52.         
  53.     paramList = [[List alloc] initCount:5];
  54.     [paramList addObject:amp];
  55.     [paramList addObject:rise];
  56.     [paramList addObject:dur];
  57.     [paramList addObject:decay];
  58.     [paramList addObject:out];
  59.     
  60.     [Inst putUgenInList:self];
  61.     
  62.     return self;
  63. }
  64.  
  65. - remove
  66. {
  67.     id cp,param;
  68.     int i;
  69.                             // make sure not connected first 
  70.     for(i = 0; i < ([paramList count]); i++) {    
  71.         param = [paramList objectAt:i];
  72.         if(cp = [param getConnectedParam]) 
  73.             return 0;
  74.     }
  75.                             // remove ugen from list
  76.     [Inst removeUgenFromList:self];
  77.     return self;
  78. }
  79.  
  80. - (NXImage *)getImage
  81. {
  82.     return image;
  83. }
  84.  
  85. - move:(NXPoint *)newloc
  86. {
  87.     location = *newloc;
  88.     [amp move:&location];
  89.     [rise move:&location];
  90.     [dur move:&location];
  91.     [decay move:&location];
  92.     [out move:&location];
  93.     return self;
  94. }
  95.  
  96. - findParamAtPoint:(NXPoint *)point
  97. {
  98.     NXRect *rect;
  99.     int i;
  100.     id param;
  101.     
  102.     for(i = 0; i < [paramList count]; i++) {
  103.         param = [paramList objectAt:i];
  104.         rect = [param getRect];
  105.         if(NXMouseInRect(point,rect,NO))
  106.             return param;
  107.     }
  108.     //printf("no param found\n");
  109.     return nil;
  110. }
  111.  
  112. - writeUgen
  113. {
  114.     BOOL ai = NO;    // amplitude input ugen
  115.     BOOL ri = NO;    // rise input ugen
  116.     BOOL di = NO;    // decay input ugen
  117.     char ampVar[10];
  118.     char riseVar[10];
  119.     char decayVar[10];
  120.     char par[20];
  121.     /*     for amp, rise, & decay input params:
  122.             see if there is input ugen
  123.                 if so, grab the output (ug?) and multiply it by this
  124.                     param value
  125.                 if not just use the param value
  126.          after all params done, write relevant code into lists
  127.             (declarations, assignments and loop statements)
  128.             i.e. ug? = oscil(,,,,);
  129.         go to output param connected ugen and call its "writeUgen"
  130.     */
  131.     int i;
  132.     int parent[4];
  133.     id param;
  134.     id ug;
  135.     id nupar;
  136.     char str[50];
  137.     
  138.     //printf("writing ugen %d\n",index);
  139.     sprintf(str,"\tfloat ug%d;\n",index);
  140.     [Inst putVarInList:str];
  141.     sprintf(str,"\tfloat rise%d;\n",index);
  142.     [Inst putVarInList:str];
  143.     sprintf(str,"\tfloat decay%d;\n",index);
  144.     [Inst putVarInList:str];
  145.     sprintf(str,"\tfloat amp%d;\n",index);
  146.     [Inst putVarInList:str];
  147.     sprintf(str,"\tfloat dur%d;\n",index);
  148.     [Inst putVarInList:str];
  149.     sprintf(str,"\tfloat q%d[9];\n",index);
  150.     [Inst putVarInList:str];
  151.     sprintf(str,"\tfloat *f1_%d;\n",index);
  152.     [Inst putVarInList:str];
  153.     sprintf(str,"\tfloat *f2_%d;\n",index);
  154.     [Inst putVarInList:str];
  155.     sprintf(str,"\tfloat out%d;\n",index);
  156.     [Inst putVarInList:str];
  157.  
  158.     for(i = 0; i < ([paramList count]-1); i++) {    
  159.         param = [paramList objectAt:i];
  160.         if(nupar = [param getConnectedParam]) {
  161.             ug = [nupar getUgen];
  162.             if([ug getWritten] == NO) { 
  163.                 parent[i] = [[ug writeUgen] getIndex];
  164.             }
  165.             else  {
  166.                 parent[i] = 0;
  167.             }
  168.             //printf("parent[%d] = %d\n",i,parent[i]);
  169.         }
  170.         else 
  171.             parent[i] = 0;
  172.         
  173.     }
  174.     
  175.     strcpy(par,[[paramList objectAt:1] getValue]);
  176.     if(par[0] == 'p' && par[1] == '[')
  177.         sprintf(str,"\trise%d = %s;\n", index, par);
  178.     else
  179.         sprintf(str,"\trise%d = %.2f;\n", index, atof(par));
  180.     [Inst putAssignInList:str];
  181.     
  182.     strcpy(par,[[paramList objectAt:2] getValue]);
  183.     if(par[0] == 'p' && par[1] == '[')
  184.         sprintf(str,"\tdur%d = %s;\n", index, par);
  185.     else
  186.         sprintf(str,"\tdur%d = %.2f;\n", index, atof(par));
  187.     [Inst putAssignInList:str];
  188.     
  189.     strcpy(par,[[paramList objectAt:3] getValue]);
  190.     if(par[0] == 'p' && par[1] == '[')
  191.         sprintf(str,"\tdecay%d = %s;\n", index, par);
  192.     else
  193.         sprintf(str,"\tdecay%d = %.2f;\n", index, atof(par));
  194.     [Inst putAssignInList:str];
  195.     
  196.     sprintf(str,"\tf1_%d = (float *)floc(2);\n", index);
  197.     [Inst putAssignInList:str];
  198.     sprintf(str,"\tf2_%d = f1_%d;\n", index,index);
  199.     [Inst putAssignInList:str];
  200.     
  201.     if(parent[0]) {     // amplitude input ugen    
  202.         //printf("input ugen to evp amp\n");
  203.         ai = YES;
  204.         sprintf(str,"\tamp%d = %.2f;\n",index, 
  205.             atof([[paramList objectAt:0] getValue]));
  206.         [Inst putAssignInList:str];
  207.         sprintf(str,"\tfloat ampai%d;\n",index);
  208.         [Inst putVarInList:str];
  209.         sprintf(str,"\t\tampai%d = amp%d * ug%d;\n",index,  
  210.             index,parent[0]);
  211.         [Inst putLoopInList:str];
  212.     }
  213.     else {                // amp preset or default
  214.         //printf("amp preset or default\n");
  215.         sprintf(str,"\tamp%d = %.2f;\n",index,atof([[paramList objectAt:0] getValue]));
  216.         [Inst putAssignInList:str];
  217.     }
  218.     if(ai)
  219.         sprintf(ampVar,"ampai%d",index);
  220.     else
  221.         sprintf(ampVar,"amp%d",index);
  222.         
  223.                         // function 2 hardwired to rise function for now
  224.     sprintf(str, "\tevset(dur%d,rise%d,decay%d,2,q%d);\n",index,index,index, index);
  225.     [Inst putAssignInList:str];
  226.     
  227.                     // output amplitude multiplier:
  228.     strcpy(par,[[paramList objectAt:4] getValue]);
  229.     if(par[0] == 'p' && par[1] == '[')
  230.         sprintf(str,"\tout%d = %s;\n",index,par);
  231.     else
  232.         sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:4] getValue]));
  233.     [Inst putAssignInList:str];
  234.  
  235.     sprintf(str,"\t\tug%d = %s(i,f1_%d,f2_%d,q%d)*amp%d*out%d;\n", index,name, index,index,index,index,index);
  236.     [Inst putLoopInList:str];
  237.     written = YES;
  238.     return self;    
  239. }
  240.  
  241.  
  242. @end
  243.